package cn.two.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.Page;

import cn.two.model.OaBuy;
import cn.two.model.OaDept;
import cn.two.model.OaSubmit;
import cn.two.model.OaUser;
import cn.two.model.Result;
import cn.two.service.OaBuyService;
@RestController  
/**
 * 1.如果一个controller里面大部分返回的都是页面的话，使用@Controller，这时返回的都是页面
 * 2.如果大部分返回的都是Json或字符串的话，就使用@RestController。如果它碰到Stirng，就返回String
 * int返回int（即基本数据类型还返回相应的数据类型），但是对象会返回json
 * 3.此时(使用@RestController)如果某方法仍要返回视图，则使用ModelAndView
 */
@RequestMapping("/OaBuy")
public class OaBuyController {
	
	@Resource
	private OaBuyService oaBuyService;
	
	/**
	 * 分页
	 * @param currPage
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/getPage")
	public Map queryOaBuyByPage(int pageNum, int pageSize,OaBuy oaBuy,HttpServletRequest request) {
		String deptId=request.getParameter("deptid");
		int deptid=Integer.parseInt(deptId);
		String subid=request.getParameter("submitid");	
		if(subid==null){subid="0";}
		int subId=Integer.parseInt(subid);
		String userUsername=request.getParameter("userUsername");
		OaUser oaUser=new OaUser();
		oaUser.setUserUsername(userUsername);
		OaDept oaDept=new OaDept();
		OaSubmit oasu= new OaSubmit();
		oaDept.setDeptId(deptid);
		oaBuy.setOaDept(oaDept);
		oasu.setSubmitId(subId);
		oaBuy.setOaSubmit(oasu);
		oaBuy.setOaUser(oaUser);
	
		Map map=new HashMap();
 		map.put("pageSize", pageSize);
 		map.put("pageNum", pageNum);
 		map.put("oaBuy", oaBuy);
 		Page page=oaBuyService.queryLikeOaBuys(map);
 		map.put("page", page);
 		map.put("totalPage", page.getPages());
 		
		return map;
	}
	
	
	@RequestMapping("/list")
	public ModelAndView seach(Model model,OaBuy oaBuy,HttpServletRequest request){
	String deptId=request.getParameter("deptid");
	String subid=request.getParameter("submitid");
	if(deptId==null&&subid==null){
		List<OaBuy> list=oaBuyService.selectAllBuy();
		model.addAttribute("buylist", list);
		return new ModelAndView("/buyList");
		
		
	}else{
		
	OaDept oaDept=new OaDept();
	OaSubmit oasu= new OaSubmit();
	int deptid=Integer.parseInt(deptId);
	int subId=Integer.parseInt(subid);
	oaDept.setDeptId(deptid);
	oaBuy.setOaDept(oaDept);
	oasu.setSubmitId(subId);
	oaBuy.setOaSubmit(oasu);
	List<OaBuy> list=oaBuyService.search(oaBuy);
	model.addAttribute("buylist", list);
	return new ModelAndView("buyList");	
	}
	
}
	/**
	 * 新增申请
	 * @return
	 */
	@RequestMapping("/adds")
	public ModelAndView adds() {
		return new ModelAndView("add");
	}
	
	/**
	 * 添加
	 * @param oaBuy
	 * @return
	 */
	@RequestMapping("addbuy")
	public Result addbuy(OaBuy oaBuy,HttpSession session){
		OaUser oaUser = (OaUser) session.getAttribute("oauser");
		OaDept oaDept = new OaDept();
		 int deptId = oaUser.getOaDept().getDeptId();
		 oaDept.setDeptId(deptId);
		oaBuy.setOaDept(oaDept);
		oaBuy.setOaUser(oaUser);
		oaBuyService.save(oaBuy);
		return new Result();
		}
	
	/**
	 * 循环添加多个对象
	 */
	
	@RequestMapping("addbuys")
	public ModelAndView addbuys(OaBuy oaBuys,HttpServletRequest request){
		OaBuy buy=new OaBuy();
		String buyDate=request.getParameter("buydate");
		int deptid=Integer.parseInt(request.getParameter("deptid"));
		int userid=Integer.parseInt(request.getParameter("userid"));
		int submitid=Integer.parseInt(request.getParameter("submitid"));
		
		
		for(int i=0;i<12;i++){
			
			String buyName=request.getParameter("buyName"+i);

			String buyNumber=request.getParameter("buyNumber"+i);
	
			String buyPrice=request.getParameter("buyPrice"+i);

			String buyUseDate=request.getParameter("buyUseDate"+i);

			String buyUse=request.getParameter("buyUse"+i);
			
			if((!("".equals(buyName))&&buyName!=null)&&
			   (!("".equals(buyNumber))&&buyNumber!=null)&&
			   (!("".equals(buyPrice))&&buyPrice!=null)&&
			   (!("".equals(buyUseDate))&&buyUseDate!=null)&&
			   (!("".equals(buyUse))&&buyUse!=null))
			{
				
				buy.setBuyName(buyName);
				buy.setBuyNumber(Integer.parseInt(buyNumber));
				buy.setBuyPrice(Double.parseDouble(buyPrice));
				buy.setBuyUseDate(buyUseDate);
				buy.setBuyUse(buyUse);
				buy.setBuyDate(buyDate);
				
				OaDept dept= new OaDept();
				dept.setDeptId(deptid);
				buy.setOaDept(dept);
				
				OaUser user =new OaUser();
				user.setUserId(userid);
				buy.setOaUser(user);
				
				OaSubmit sub= new OaSubmit();
				sub.setSubmitId(submitid);
				buy.setOaSubmit(sub);
				
				oaBuyService.save(buy);
			}else{return new ModelAndView("buyList");}
		}
		
		return new ModelAndView("buyList");}
	
	
	
	/**
	 * 根据id删除
	 * @param id
	 * @return
	 */
	
	@RequestMapping("/del")
	public Result delete(OaBuy oaBuy){
		oaBuyService.delete(oaBuy);
		return new Result();
		
	}
	
	
	/**
	 * 查询单个用户详细信息
	 * 
	 * @return
	 */
	@RequestMapping("/editpage")
	public ModelAndView findOaBuy(OaBuy oaBuy){
		
		OaBuy oabuy = oaBuyService.findOaBuy(oaBuy);
        //Map<String,Object> rsMap = new HashMap<String,Object>();
        //rsMap.put("data", oabuy);
		ModelAndView mv =new ModelAndView();
		mv.addObject("oabuy",oabuy);
		//return new ModelAndView("buyUpdate",rsMap);
		mv.setViewName("buyUpdate");
		return mv;
		
	}
	
	/**
	 * 编辑订单信息
	 * @param oaBuy
	 * @return
	 */
	@RequestMapping("/edit")
	public Result editOaBuy(OaBuy oaBuy){
		oaBuyService.update(oaBuy);
		return new Result();
	}

	public OaBuyService getOaBuyService() {
		return oaBuyService;
	}

	public void setOaBuyService(OaBuyService oaBuyService) {
		this.oaBuyService = oaBuyService;
	}
	
	
	@RequestMapping("/test")
	public ModelAndView test() {
		return new ModelAndView("addlist");
	}

}
